package com.qq.springbootdemo.controller;

import com.qq.springbootdemo.common.exception.InsufficientBalanceException;
import com.qq.springbootdemo.common.exception.WalletNotFoundException;
import com.qq.springbootdemo.domain.entity.TransactionEntity;
import com.qq.springbootdemo.domain.entity.WalletEntity;
import com.qq.springbootdemo.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/wallets")
public class WalletController {
    
    @Autowired
    private WalletService walletService;
    
    @PostMapping
    public ResponseEntity<WalletEntity> createWallet(@RequestParam Long userId,
                                                     @RequestParam String currency) {
        WalletEntity wallet = walletService.createWallet(userId, currency);
        return ResponseEntity.ok(wallet);
    }
    
    @GetMapping("/{walletId}/balance")
    public ResponseEntity<BigDecimal> getBalance(@PathVariable Long walletId) {
        BigDecimal balance = walletService.getBalance(walletId);
        return ResponseEntity.ok(balance);
    }
    
    @PostMapping("/{walletId}/deposit")
    public ResponseEntity<String> deposit(@PathVariable Long walletId, 
                                        @RequestParam BigDecimal amount) {
        try {
            walletService.deposit(walletId, amount);
            return ResponseEntity.ok("Deposit successful");
        } catch (WalletNotFoundException e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }
    }
    
    @PostMapping("/{walletId}/withdraw")
    public ResponseEntity<String> withdraw(@PathVariable Long walletId, 
                                         @RequestParam BigDecimal amount) {
        try {
            walletService.withdraw(walletId, amount);
            return ResponseEntity.ok("Withdrawal successful");
        } catch (WalletNotFoundException e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        } catch (InsufficientBalanceException e) {
            throw new RuntimeException(e);
        }
    }
    
    @PostMapping("/transfer")
    public ResponseEntity<String> transfer(@RequestParam Long fromWalletId,
                                         @RequestParam Long toWalletId,
                                         @RequestParam BigDecimal amount) {
        try {
            walletService.transfer(fromWalletId, toWalletId, amount);
            return ResponseEntity.ok("Transfer successful");
        } catch (WalletNotFoundException e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        } catch (InsufficientBalanceException e) {
            throw new RuntimeException(e);
        }
    }
    
    @GetMapping("/{walletId}/transactions")
    public ResponseEntity<List<TransactionEntity>> getTransactions(@PathVariable Long walletId) {
        List<TransactionEntity> transactions = walletService.getTransactionHistory(walletId);
        return ResponseEntity.ok(transactions);
    }
}